<%@ page import="com.tm.controller.InstrumentController" %>
<%@ page import="com.tm.pojo.Instrument" %>
<%@ page import="com.tm.controller.PriceController" %>
<%@ page import="com.tm.pojo.Price" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<jsp:include page="header.jsp"></jsp:include>

<%
if(request.getParameter("instrumentId") == null || request.getParameter("instrumentId").trim().length()==0) {
	%>
	<jsp:forward page="prices.jsp"/> 
	<%
}

boolean isAdmin = false;
UserService userService = UserServiceFactory.getUserService();
if(userService.isUserLoggedIn() && userService.getCurrentUser().getNickname().equals("vahe@andonians.com")) {
	isAdmin = true;
}

Instrument instrument = InstrumentController.getInstance().getInstrument(Long.parseLong(request.getParameter("instrumentId")));
if (instrument != null) {
	PriceController pc = PriceController.getInstance();
	pc.setInstrumentId(instrument.getInstrumentId());
	List<Price> priceList = pc.getPrices();
	Iterator<Price> prices = priceList.iterator();
	%>
	<h1><%=instrument.getName()%></h1>
	
	<%
	if (isAdmin) {
		%>
		<script type="text/javascript" src="dateTime.js"></script>
		<script type="text/javascript" src="priceHistory.js"></script>
		<form action="">
			<table border="0" class="form-table" >
				<tr>
					<td class="form-label-cell">Date</td>
					<td class="form-input-cell"><input name="date" id="date" type="text" size="20" value="" class="text-input">&nbsp;dd/MM/yy&nbsp;(empty for today)</td>
				</tr>
				<tr>
					<td class="form-label-cell">Price</td>
					<td class="form-input-cell"><input name="price" id="price" type="text" size="20" value="" class="text-input"></td>
					<td class="form-error"><label class="error" for="price" id="price_error">This field is required.</label></td>
				</tr>
				<tr style="display:none">
					<td class="form-input-cell"><input name="instrumentId" id="instrumentId" type="text" value="<%=instrument.getInstrumentId()%>" class="text-input"></td>
				</tr>
				<tr>
					<td class="form-label-cell">&nbsp;</td>
					<td class="form-input-cell"><input type="submit" name="submit" class="button new-button" id="submit_btn" value="Save" /></td>
					<td class="form-error"></td>
				</tr>
			</table>
		</form>
		<p></p>
		<%	
	}
	%>
	<table border="0" id="tblPrices">
		<tr><th>Date</th><th>Price</th><th>Action</th></tr>
		<%
		while(prices.hasNext()) {
			Price p = prices.next();
		%>
			<tr id="row<%=p.getPriceId()%>">
				<td class="numeric"><%=p.getFormatedDate()%></td>
				<td class="numeric"><%=p.getFormatedPrice()%></td>
				<td><button class="button delete-button" onclick="deletePrice(<%=p.getPriceId()%>)" id="btn<%=p.getPriceId()%>">Delete</button></td>
				<td style="display:none"><%=p.getDate().getTime() %></td>
			</tr>
		<%
		}
		%>
	</table>

	<%
}	
%>

<jsp:include page="footer.jsp"></jsp:include>